﻿using HardcoreGame.Common.Players;
using HardcoreGame.Common.Systems;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria;
using Terraria.Audio;
using Terraria.GameContent;
using Terraria.GameContent.UI.Elements;
using Terraria.ID;
using Terraria.Localization;
using Terraria.ModLoader;
using Terraria.UI;

namespace HardcoreGame.Common.UI
{
    public class AgeUI : UIState
    {
        private UIText _ageText;
        private UIElement _slider;
        private float _sliderPosition;
        private bool _dragging;
        private const int MinAge = 1;
        private const int MaxAge = 130;

        public override void OnInitialize()
        {
            var panel = new UIPanel();
            panel.Width.Set(400, 0);
            panel.Height.Set(180, 0);
            panel.HAlign = 0.5f;
            panel.VAlign = 0.4f;
            panel.BackgroundColor = new Color(63, 82, 151) * 0.8f;
            var title = new UIText(Language.GetText("Mods.HardcoreGame.UI.13"))
            {
                HAlign = 0.5f
            };
            title.Top.Set(15, 0);
            panel.Append(title);
            _ageText = new UIText("18岁")
            {
                HAlign = 0.5f
            };
            _ageText.Top.Set(45, 0);
            panel.Append(_ageText);
            var sliderBg = new UIElement();
            sliderBg.Width.Set(300, 0);
            sliderBg.Height.Set(20, 0);
            sliderBg.HAlign = 0.5f;
            sliderBg.Top.Set(80, 0);
            panel.Append(sliderBg);
            _slider = new UIElement();
            _slider.Width.Set(20, 0);
            _slider.Height.Set(20, 0);
            sliderBg.Append(_slider);
            var confirmButton = new UITextPanel<LocalizedText>(Language.GetText("Mods.HardcoreGame.UI.14"));
            confirmButton.Width.Set(100, 0);
            confirmButton.Height.Set(30, 0);
            confirmButton.HAlign = 0.5f;
            confirmButton.Top.Set(110, 0);
            confirmButton.OnLeftClick += (_, _) =>ConfirmAge();
            panel.Append(confirmButton);

            Append(panel);
            _sliderPosition = 0.5f;
            UpdateAgeText();
        }
        private void ConfirmAge()
        {
            int age = (int)(MinAge + _sliderPosition * (MaxAge - MinAge));
            Main.LocalPlayer.GetModPlayer<AgePlayer>().SetAge(age);
            CloseUI();
        }

        private static void CloseUI()
        {
            SoundEngine.PlaySound(SoundID.MenuClose);
            ModContent.GetInstance<AgeUISystem>().HideAgeSelection();
            if (Main.LocalPlayer.active)
            {
                Main.LocalPlayer.GetModPlayer<AgePlayer>()._hasShownThisSession = true;
            }
        }

        private void UpdateAgeText()
        {
            int age = (int)(MinAge + _sliderPosition * (MaxAge - MinAge));
            _ageText.SetText($"{age}");
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            if (_dragging)
            {
                var dimensions = _slider.Parent.GetDimensions();
                _sliderPosition = MathHelper.Clamp((Main.mouseX - dimensions.X) / dimensions.Width, 0f, 1f);
                UpdateAgeText();

                if (!Main.mouseLeft)
                {
                    _dragging = false;
                }
            }
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            base.Draw(spriteBatch);

            var dimensions = _slider.Parent.GetDimensions();
            var sliderPos = new Vector2(
                dimensions.X + _sliderPosition * dimensions.Width,
                dimensions.Y + dimensions.Height / 2f);
            spriteBatch.Draw(
                TextureAssets.MagicPixel.Value,
                new Rectangle((int)dimensions.X, (int)(dimensions.Y + dimensions.Height / 2 - 2),
                    (int)dimensions.Width, 4),
                Color.White);
            spriteBatch.Draw(
                TextureAssets.MagicPixel.Value,
                new Rectangle((int)(sliderPos.X - 10), (int)(sliderPos.Y - 10), 20, 20),
                Color.LightGray);
        }

        public override void LeftMouseDown(UIMouseEvent evt)
        {
            base.LeftMouseDown(evt);
            if (evt.Target == _slider || evt.Target == _slider.Parent)
            {
                _dragging = true;
            }
        }
        public override void OnDeactivate()
        {
            if (Main.LocalPlayer.active)
            {
                Main.LocalPlayer.GetModPlayer<AgePlayer>()._hasShownThisSession = true;
            }
        }
    }
}